% template and signal initializing
step = 0.01;
% template - canonacal gaussian
[xpsi psi] = template3(step);
% SSD theshold level
SSDThresholdLevel = .03:.01:.07;
% maximum derivation in
trustedDerivation = 0.1;
% number of gaussians in experiments
gaussiansNumber = 10;
% S - number of statistic iterations (size of samlpe)
S = 100;

% ================= clean signal matching =================================

% effectivity results initialization

SSDFindedGaussiansNumber = zeros(1, length(SSDThresholdLevel));
SSDWrongGaussiansNumber = zeros(1, length(SSDThresholdLevel));

for i = 1: length(SSDThresholdLevel)
    for count = 1:S
        % statistic experiment
        % signal generating - sum of gaussians that are generated randomly
        [x sign means fmeans] = signal8(step, gaussiansNumber);
        % getting maximums of clean signal
        [xSignMax SignMax] = maxEdges(x, sign, .8 * min(fmeans));
        
        % ===== Sum of squared differences squares =======================
        ssd = SSD(sign, psi);
        scaledSSDEffectogram = SSDEffectogramScaling(x, ssd, xpsi);
        % getting local minimums of SSD effectogram
        [trustedMinimums ~] = minEdges(x, scaledSSDEffectogram, SSDThresholdLevel(i));
        % getting number of finded gaussians
        numberFindedGaussians = searchEffectivity(xSignMax, trustedDerivation, trustedMinimums);
        SSDFindedGaussiansNumber(i) = SSDFindedGaussiansNumber(i) + numberFindedGaussians;
        % getting number of wrong-found gaussians
        SSDWrongGaussiansNumber(i) = SSDWrongGaussiansNumber(i)...
            + (length(trustedMinimums) - numberFindedGaussians);
    end;
end;

% getting average values
SSDFindedGaussiansNumber = SSDFindedGaussiansNumber / S;

SSDWrongGaussiansNumber = SSDWrongGaussiansNumber / S;

% plotting results
figure;
plot(SSDThresholdLevel, SSDFindedGaussiansNumber);
grid on;
title('Template Matching methods for clean signal');
legend('Sum of squared differences');
xlabel('Trust level to Sum of squared differences method');
ylabel('Number of found gaussians');

figure;
plot(SSDThresholdLevel, SSDWrongGaussiansNumber);
grid on;
title('Template Matching methods for clean signal');
legend('Sum of squared differences');
xlabel('Trust level to Sum of squared differences method');
ylabel('Number of wrong-found gaussians');